﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebEntities;
using WebEntities.Models.WebInfo;

namespace com.subb.WebInfo
{
    public class WI_LeaveMsgDAL
    {
        public List<WI_LeaveMsg> GetAllLeaveMsg(int start, int limit, out int ttlCount)
        {
            ttlCount = 0;
            using (SugardbEntities ctx = new SugardbEntities())
            {
                ctx.ContextOptions.LazyLoadingEnabled = false;
                var query = ctx.WI_LeaveMsg.OrderByDescending(c => c.MsgID);
                ttlCount = query.Count();
                List<WI_LeaveMsg> msglist = query.Skip(start).Take(limit).ToList();
                return msglist;
            }
        }

        public List<WI_LeaveMsg> GetAllLeaveMsg(int typeId, int start, int limit, out int ttlCount)
        {
            ttlCount = 0;
            using (SugardbEntities ctx = new SugardbEntities())
            {
                ctx.ContextOptions.LazyLoadingEnabled = false;
                var query = ctx.WI_LeaveMsg.Where(c => c.TypeID == typeId).OrderByDescending(c => c.MsgID);
                ttlCount = query.Count();
                List<WI_LeaveMsg> msglist = query.Skip(start).Take(limit).ToList();
                return msglist;
            }
        }

        public List<WI_LeaveMsg> GetLeaveMsgByAudi(int start, int limit, out int ttlCount)
        {
            ttlCount = 0;
            SugardbEntities ctx = new SugardbEntities();
            var query = ctx.WI_LeaveMsg.Where(c => c.IsPublic == true).OrderByDescending(c => c.MsgID);
            ttlCount = query.Count();
            List<WI_LeaveMsg> msglist = query.Skip(start).Take(limit).ToList();
            return msglist;

        }

        public WI_LeaveMsg GetLeaveMsg(int msgId)
        {
            using (SugardbEntities ctx = new SugardbEntities())
            {
                ctx.ContextOptions.LazyLoadingEnabled = false;
                WI_LeaveMsg lmsg = ctx.WI_LeaveMsg.SingleOrDefault(c => c.MsgID == msgId);
                return lmsg;
            }
        }

        public void CreateLeaveMsg(LeaveMsg v_lmsg)
        {
            using (SugardbEntities ctx = new SugardbEntities())
            {
                WI_LeaveMsg wilMsg = new WI_LeaveMsg
                {
                    TypeID = v_lmsg.TypeID,
                    MsgSubject = v_lmsg.MsgSubject,
                    MsgBody = v_lmsg.MsgBody,
                    NeedContact = v_lmsg.NeedContact,
                    ContactMan = v_lmsg.ContactMan,
                    Mobile = v_lmsg.Mobile,
                    Email = v_lmsg.Email,
                    QQNum = v_lmsg.QQNum,
                    Keywords = v_lmsg.Keywords,
                    CreateTime = DateTime.Now,
                    IsReply = v_lmsg.IsReply,
                    IsPublic = v_lmsg.IsPublic,
                    Auditor = v_lmsg.Auditor
                };
                ctx.WI_LeaveMsg.AddObject(wilMsg);
                ctx.SaveChanges();
            }
        }

        public bool RemoveLeaveMsg(int msgId)
        {
            bool result = false;
            using (SugardbEntities ctx = new SugardbEntities())
            {
                WI_LeaveMsg wilMsg = ctx.WI_LeaveMsg.SingleOrDefault(c => c.MsgID == msgId);
                if (wilMsg != null)
                {
                    ctx.WI_LeaveMsg.DeleteObject(wilMsg);
                    ctx.SaveChanges();
                    result = true;
                }
            }
            return result;
        }

        public bool AuditLeaveMsg(int msgId, bool isPub, string auditor)
        {
            bool result = false;
            using (SugardbEntities ctx = new SugardbEntities())
            {
                WI_LeaveMsg wilMsg = ctx.WI_LeaveMsg.SingleOrDefault(c => c.MsgID == msgId);
                if (wilMsg != null)
                {
                    wilMsg.IsPublic = isPub;
                    wilMsg.Auditor = auditor;
                    ctx.SaveChanges();
                    result = true;
                }
            }
            return result;
        }
    }
}
